# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import os
from selenium.webdriver.common.keys import Keys
import time
import xlrd
import xlwt


class RowInfo:
    type = ''
    category = ''
    product = ''
    brand = ''
    unit = ''
    id = ''
    zrid = ''

    def string(self):
        return 'type: %s, category: %s, product: %s, brand: %s, unit: %s, id: %s, zrid: %s\n' % \
               (self.type, self.category, self.product, self.brand, self.unit, self.id, self.zrid)


filePath = '/Volumes/WWW/123.xls'

table = xlwt.Workbook()
sheet = table.add_sheet("sheet0", True)

num = 0
def save(row):
    global num
    print(row.string())
    sheet.write(num, 0, row.type)
    sheet.write(num, 1, row.category)
    sheet.write(num, 2, row.product)
    sheet.write(num, 3, row.brand)
    sheet.write(num, 4, row.unit)
    sheet.write(num, 5, row.id)
    sheet.write(num, 6, row.zrid)

    table.save(filePath)
    num += 1
    print('row number is %d' % num)

def contains_row(rows, row):
    for r in rows:
        # if (r.type == row.type and r.category == row.category and r.product == row.product and r.brand == row.brand
        #     and r.unit == row.unit):
        #     return True
        if r.zrid == row.zrid:
            return True
    return False



driverPath = os.path.abspath('phantomjs')
browser = webdriver.PhantomJS(driverPath)


browser.get('http://dev.bjmaxinfo.net/mall/front/login')
try:
    name = browser.find_element_by_xpath('//*[@id="user"]')
    name.send_keys('17186761918')

    pwd = browser.find_element_by_xpath('//*[@id="password"]')
    pwd.send_keys('123456')
    # 登录
    browser.find_element_by_xpath('//*[@id="submit_0"]').click()

    curPage = 95
    try:
        while True:

            if curPage == 99999:
                break
            curPage += 1

            for i in range(1, 21):

                browser.get('http://dev.bjmaxinfo.net/mall/manage/order/rental/ordermainindex')
                browser.find_element_by_xpath('//*[@id="numCode"]').send_keys('PG')
                browser.find_element_by_xpath('//*[@id="filterForm"]/div[6]/div/button[1]').click()

                if curPage != 1:
                    if curPage <= 11:
                        path = '/html/body/div[1]/div[3]/a[' + str(curPage - 1) + ']'
                        browser.find_element_by_xpath(path).click()
                    elif curPage > 11:
                        path = '/html/body/div[1]/div[3]/a[10]'
                        browser.find_element_by_xpath(path).click()

                        while True:
                            alist = browser.find_elements_by_xpath('/html/body/div[1]/div[3]/a')
                            flag = 0
                            for a in alist:
                                if a.text == str(curPage):
                                    a.click()
                                    flag = 1
                                    break
                            if flag == 1:
                                break
                            else:
                                alist[-3].click()


                print("page: %d, current: %d" % (curPage, i))

                try:
                    path = '/html/body/div[1]/div[2]/table/tbody/tr[' + str(i) + ']/td[10]/a[1]'
                    browser.find_element_by_xpath(path).click()


                    # 问题产品
                    button = browser.find_element_by_xpath('/html/body/div[1]/div[1]/span[2]/a[1]')
                    if button.text == '问题产品':
                        button.click()

                        try:
                            k = 0
                            while True:
                                k += 1
                                row = RowInfo()
                                path = '//*[@id="entityForm"]/div[2]/table/tbody/tr[' + str(k) + ']/td[2]'
                                row.type = browser.find_element_by_xpath(path).text

                                path = '//*[@id="entityForm"]/div[2]/table/tbody/tr[' + str(k) + ']/td[3]'
                                row.category = browser.find_element_by_xpath(path).text

                                path = '//*[@id="entityForm"]/div[2]/table/tbody/tr[' + str(k) + ']/td[4]'
                                row.product = browser.find_element_by_xpath(path).text

                                path = '//*[@id="entityForm"]/div[2]/table/tbody/tr[' + str(k) + ']/td[5]'
                                row.brand = browser.find_element_by_xpath(path).text

                                path = '//*[@id="entityForm"]/div[2]/table/tbody/tr[' + str(k) + ']/td[6]'
                                row.unit = browser.find_element_by_xpath(path).text

                                path = '//*[@id="entityForm"]/table/tbody/tr[1]/td[1]/a/font'
                                row.id = browser.find_element_by_xpath(path).text

                                path = '//*[@id="entityForm"]/div[2]/table/tbody/tr[' + str(k) + ']/td[8]/input[2]'
                                row.zrid = browser.find_element_by_xpath(path).get_attribute('value')

                                save(row)

                        except (BaseException) as error:
                            print(error)
                            print('---------')
                            # curPage = 99999
                            # break


                    else:
                        print('there is no problem product')


                except(BaseException) as error:
                    print('go to detail error')



    except(BaseException) as error:
        print(error)

except(BaseException) as error:
    print(error)



